﻿/**
 * --------------------------------------
 * FileName: work.2.c
 * Content: 
 *      原地倒置单链表
 * 
 * Auther:  杨则瑞
 * Encode:  utf-8 | CRLF
 * --------------------------------------
 */

#include<stdio.h>

#include<mono_list.h>
#include<mono_list_int_ext.h>

#include<debuging.h>


const char * title = 
"====================================================================== \n"\
" WORK 2 of Chapter 2                                                   \n"\
"---------------------------------------------------------------------- \n"\
" Goal: Reversify a Single linked list with no extra Memory usage.\n"
"====================================================================== \n";


/**
 * 原地反转链表 O(n)
 * 简述: 遍历链表，弹出元素，插入到 _header 之后
 */
void monoList_ext_reversify(monoList_t * self)
{
    monoList_node_t *p = self->_header->next;
    if(p == NULL)
    {
        return; // 特判0长数组
    }
    while(p->next != NULL)
    {
        monoList_node_t * fp = p->next;
        p->next = fp->next;
        fp->next = self->_header->next;
        self->_header->next = fp;
    }
    return;
}


#ifdef DESKTOP_MODE /*===========================================================*/
int main()
{
    monoList_t mono_list;
    monoList_init(&mono_list,sizeof(comparable_t));

    printf(title);

    printf("Please enter the quantity of input numbers:\n");


    // 从stdin读输入数据数量
    int n;
    int qod =scanf("%d%*[^\n]",&n);

    
    if(qod != 1)
    {
        printf("Expects 1 integers, reads %d.\nExit.\n",qod);
        return -1;
    }

    if(n < 0)
    {
        printf("Fault: Negative n.\nExit.\n");
        return -1;
    }


    if(n == 0)
    {
        printf("Note: n is 0, list will be empty\n");
    }

    if(n != 0)
    {
        printf("Please enter integer numbers:\n");
    }


    // 存入LList。
    monoList_ext_from_file (&mono_list,stdin,n);



    printf("Number of valid data: %d.\n", monoList_length(&mono_list));
    printf("Current state:\n");

    //当前状态
    monoList_ext_to_file(&mono_list, stdout);
    putchar('\n');



    //反转
    monoList_ext_reversify(&mono_list);
    

    printf("Flipped state:\n", monoList_length(&mono_list));

    //输出
    monoList_ext_to_file(&mono_list, stdout);
    putchar('\n');


    system("pause");


    //析构
    monoList_uninit(&mono_list);


    return 0;
}

#else

int main()
{
    monoList_t mono_list;
    monoList_init(&mono_list,sizeof(comparable_t));


    // 从stdin读输入数据数量
    int n;
    int qod =scanf("%d%*[^\n]",&n);

    
    if(qod != 1)
    {
        printf("Expects 1 integers, reads %d.\nExit.\n",qod);
        return -1;
    }

    if(n < 0)
    {
        printf("Fault: Negative n.\nExit.\n");
        return -1;
    }


    // 存入LList。
    monoList_ext_from_file (&mono_list,stdin,n);


    //反转
    monoList_ext_reversify(&mono_list);


    //输出
    monoList_ext_to_file(&mono_list, stdout);
    putchar('\n');


    //析构
    monoList_uninit(&mono_list);


    return 0;
}
#endif /*========================================================================*/
